import { isUnde, isEmpty, isEmptyObj, isString } from "../validate";
import { ktlapi } from "@/utils"

export function useKtluser(){
	// 最近一次登录时间
	const lastconnectKey = 'system:mp:last-connect-time:key';
	// 记住用户信息
	const userKey = 'system:mp:user:key';
	
	/** 设置用户信息 */
	function setUser(data){
		if (!isEmpty(data)) {
			uni.setStorageSync(userKey, data);
		} 
		//console.log(data);
		// 链式调用
		// return this
	}
	/** 获取用户信息 */
	function getUser(valid){
		//console.log('getUser')
		// valid 为 undefined 或者 true 时，校验登录超时
		valid = isUnde(valid) || valid;
		if (valid) {
			if(!logined()){
				logoff()
				return;
			}
		}
		// 获取用户数据
		const data = uni.getStorageSync(userKey);
		return isEmpty(data) ? null : data;
	}
	/** 删除用户信息 */
	function delUser(){
		uni.removeStorageSync("ReviewUUID");
		uni.removeStorageSync("ReviewAccountID");
		uni.removeStorageSync(userKey);
	}
	// 用户登录校验
	function logined(){
		// 获取用户数据
		const data = uni.getStorageSync(userKey);
		// console.log("-----data",data) 
		if(data && data.AccountID){
			//console.log(data); 
			let Preiod = data.Period;
			const system = uni.getSystemInfoSync()
			
			// console.log(system);
			// console.log(Preiod);
			if (typeof Preiod === 'string'){
				  if(system.osName === 'ios'){
					  Preiod = Preiod.replace(/-/g, '/');
				  } else {
					  Preiod = Preiod.replace(/T/g, ' ');
				  }
			}
			Preiod = Preiod.replace(/T/g, ' ');
			// console.log(Preiod);
			// console.log(new Date(Preiod) +":"+ new Date());
			// console.log(new Date(Preiod) > new Date());
			
			if(new Date(Preiod) > new Date()){
				return true;
			}
		}
		
		return false;
	}
	/** 退出登录 */
	function logoff(){
		delUser();
		uni.hideLoading();
		setTimeout(() => {
			uni.redirectTo({
				url: '/pages/login/login'
			})
		}, 500)
	}
	/** 获取用户基础信息 */
	function BaseInfo(){
		if(logined()){
			const userData = getUser(true);
			if(userData){ 
				return {
					"AccountID":userData["AccountID"],
					"Name":userData["Name"],
				}
			}
		}
		return {};
	}
	// 设置登录时间
	function setLastConnectTime(){
		uni.setStorageSync(lastconnectKey, new Date())
	}
	// 获取登录时间
	function getLastConnectTime(){
		const data = uni.getStorageSync(lastconnectKey);
		if (!isEmpty(data)) {
			return data;
		}
	}
	function update(key, data) {
		const u = getUser(false)
		u[key] = data;
		return setUser(u);
	}
	function Roles(){ 
		if(logined()){
			return getUser(false).Roles; 
		}else{
			return [];
		}
	}
	function hasRole(roleid){
		const roles = Roles();
		// console.log('roles',roles );
		if(Array.isArray(roleid)){
			// console.log('Array')
			return roleid.some(item=>roles.map(r=>r.RoleId).includes(item))
		}
		
		if(!isEmpty(roleid)){
			return roles.map(r => r.RoleId).includes(+roleid)
		}
		
		return false;
	}
	function valid(){
		return !isEmptyObj(getUser(false))
	}
	 
	
	return {
		setUser,
		getUser,
		delUser,
		logined,
		logoff,
		BaseInfo,
		setLastConnectTime,
		getLastConnectTime,
		update,
		Roles,
		hasRole,
		valid
	}
}


export default useKtluser();